package com.gxuwz.leaveSystem.controller;


import com.gxuwz.leaveSystem.entity.UserType;
import com.gxuwz.leaveSystem.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
public class InitialController {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) applicationContext.getBean("userService");

    /**
     * 登录
     * 角色判断
     * @param username
     * @param password
     * @param userType
     * @return 视图
     */
    @RequestMapping("/login")
    public ModelAndView login(String username,String password,String userType){
        ModelAndView mv = new ModelAndView();
        int num = userService.checkUser(username,password,userType);
        UserType ut = new UserType("1",userType);
        if (num ==1 && userType.equals("admin")){
            mv.addObject("ut",ut);
            mv.setViewName("init/index");
        }else if (num ==1 && userType.equals("instructor")){
            mv.addObject("ut",ut);
            mv.setViewName("init/index");
        }else if (num ==1 && userType.equals("student")){
            mv.addObject("ut",ut);
            mv.setViewName("init/index");
        }
        return mv;
    }

    /**
     * 刷新页面
     * 转到了根路径
     * @return /
     */
    @GetMapping("/init")
    public ModelAndView init(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/");
        return mv;
    }

    /**
     * 退出登录
     * @return 视图
     */
    @RequestMapping("/loginOut")
    public ModelAndView loginOut(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("init/login");
        return modelAndView;
    }


}
